//
//  HomeHeadView.m
//  WanCaiC8
//
//  Created by 罗先森 on 2018/8/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "HomeHeadView.h"
#import "TGAdditions.h"
@interface HomeHeadView ()

@property(nonatomic,strong)NSArray *btnContent;
@property(nonatomic,strong)NSArray *btnImage;
@end


@implementation HomeHeadView{
    
    
    NSMutableArray<UIButton *> *_btnList;
    
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        
    }
    return self;
}
- (NSArray *)btnContent
{
    if (!_btnContent) {
        
        _btnContent = @[@"开奖",@"计划",@"高手",@"资讯"];
       
    }
    return _btnContent;
}

- (NSArray *)btnImage
{
    if (!_btnImage) {
        
        _btnImage = @[@"open",@"plan",@"gaoshou",@"zixun"];
    }
    return _btnImage;
}


-(void)setupUI{
    
    self.backgroundColor = DEFAULTBGCOLOR;
    //    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    
    //ImageView
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"homebanner1"]];
    [bgView addSubview: imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(bgView);
        make.height.mas_equalTo(@180);
    }];
    
    
    //按钮
    
    _btnList = [NSMutableArray array];
    
    
    UIView *btnView = [[UIView alloc] initWithFrame:CGRectZero];
    
    
    CALayer *layer = [btnView layer];
    layer.shadowOffset = CGSizeMake(0, 5); //(0,0)时是四周都有阴影
    layer.shadowColor = [UIColor blackColor].CGColor;
    layer.shadowOpacity = 0.1;
    
    
    btnView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(10);
        make.right.equalTo(bgView).offset(-10);
        make.height.mas_equalTo(80);
        make.top.equalTo(imageView.mas_bottom).offset(-20);
    }];
    
    //4个按钮
    for (int i = 0; i<4; i++) {
        
        UIButton *btn = [self buttonWithTitle:self.btnContent[i] andImageName:self.btnImage[i] andTag:1001+i];
        [btnView addSubview:btn];
        [_btnList addObject:btn];
    }
    
    [_btnList  mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:10 tailSpacing:10];
    [_btnList  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnView).offset(10);
        make.bottom.equalTo(btnView).offset(-10);
        
    }];
    
    
    
    //通知试图
    UIView *notificationView = [[UIView alloc] init];
    notificationView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:notificationView];
    [notificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(btnView.mas_bottom).offset(10);
        make.height.mas_equalTo(35);
    }];
    
    UIView *imageBgView = [[UIView alloc] init];
    imageBgView.backgroundColor = [UIColor whiteColor];
    [notificationView addSubview:imageBgView];
    
    [imageBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(notificationView);
        make.width.mas_equalTo(55);
    }];
    
    UIImageView *notificationImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notification"]];
    
    [imageBgView addSubview:notificationImageView];
    
    [notificationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(imageBgView);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    _announcementLab=[[QXLable alloc] initGaryText];
    
    [notificationView insertSubview:_announcementLab belowSubview:imageBgView ];

    //线条
    
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=DEFAULTBGCOLOR;
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).with.offset(5);
        make.right.equalTo(bgView).with.offset(-5);
        make.top.equalTo(notificationView.mas_bottom);
        make.height.mas_equalTo(@.7);
    }];
    

    
    
    
   
}



-(UIButton *)buttonWithTitle:(NSString *)title andImageName:(NSString *)imgName andTag:(NSInteger)tag{

    UIButton *btn = [UIButton new];
    btn.tag = tag;
    
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    NSAttributedString *attr = [NSAttributedString tg_imageTextWithImage:[UIImage imageNamed:imgName] imageWH:30 title:title fontSize:14 titleColor:DEFAULTTEXTBLACKCOLOR spacing:8];
    
    [btn setAttributedTitle:attr forState:UIControlStateNormal];

    btn.titleLabel.numberOfLines = 0;
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    return btn;
}

-(void)buttonClick:(UIButton *)button{
    
    NSLog(@"--%ld",(long)button.tag);
    
    if([self.delegate respondsToSelector:@selector(commonFunctionWithButtonTag:)]){
        [self.delegate commonFunctionWithButtonTag: button.tag];
    }else{
        NSLog(@"代理没有实现方法");
    }
    
}


@end
